//
//  TTLiveItem.swift
//  TTProduct
//
//  Created by Toj on 3/24/21.
//  Copyright © 2021 TTProduct. All rights reserved.
//

import Cocoa

class TTLiveItem: NSCollectionViewItem {
    
    var style: TTLiveItemStyle = .brief {
        willSet {
            if newValue == style { return }
            
            if newValue == .detailed {
                detailedView = TTLiveDetailedView()
                return
            }
            
            briefView = TTLiveBriefView()
        }
    }
    
    var model: NSObject? {
        didSet {
            currentView?.model = model
        }
    }
    
    // 背景View: 视频流本地/ 远端状态等状态
    private lazy var backgroundView: TTLiveBackgroundView = {
        let view = TTLiveBackgroundView()
        return view
    }()
    
    // 直播: 展示视频流
    private lazy var videoView: NSView = {
        let view = NSView()
        return view
    }()
    
    private var currentView: TTLiveStyleView? {
        willSet {
            currentView?.removeFromSuperview()
            guard let newView = newValue else { return }
            view.addSubview(newView)
        }
    }
    
    // detailed
    private var detailedView: TTLiveDetailedView? {
        didSet {
            currentView = detailedView
            setupMakeLayoutDetailed()
        }
    }
    
    // brief
    private var briefView: TTLiveBriefView? {
        didSet {
            currentView = briefView
            setupMakeLayoutBrief()
        }
    }
    
    private var trackingArea: NSTrackingArea?
    override func loadView() {
        view = NSView()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupMakeAddSubviews()
        setupMakeLayoutSubviews()
        setupMakeUI()
    }
    
    override func viewDidLayout() {
        super.viewDidLayout()
        
        detailedView?.trackingAreas.forEach { (area) in
            view.removeTrackingArea(area)
        }
        let aOptions: NSTrackingArea.Options = [.mouseEnteredAndExited,
                                                .mouseMoved,
                                                .cursorUpdate,
                                                .activeAlways
        ]
        trackingArea = NSTrackingArea(rect: view.bounds, options: aOptions, owner: self, userInfo: nil)
        detailedView?.addTrackingArea(trackingArea!)
    }
}

// MARK: - Class Override Event
extension TTLiveItem {
    
    // 鼠标进入
    override func mouseEntered(with event: NSEvent) {
        super.mouseEntered(with: event)

        setupHoverUI(is: false)
    }

    // 鼠标离开
    override func mouseExited(with event: NSEvent) {
        super.mouseExited(with: event)

        setupHoverUI(is: true)
    }
    
    override func prepareForReuse() {
        super.prepareForReuse()
    }
}

// MARK: - Private
extension TTLiveItem {
    private func setupHoverUI(is hidden: Bool) {
        detailedView?.zoomButton.isHidden = hidden
    }
}

// MARK: - 点击事件
extension TTLiveItem {
   
//    @objc private func didTapPersistent(sender: NSButton) {
//
//    }
}

// MARK: - setup
extension TTLiveItem {
    
    private func setupMakeAddSubviews() {
        
        view.addSubview(backgroundView)
        view.addSubview(videoView)
    }
    
    private func setupMakeLayoutSubviews() {
        
        backgroundView.snp.remakeConstraints { (make) in
            make.edges.equalTo(view)
        }
    }
    
    private func setupMakeLayoutDetailed() {
        
        videoView.snp.remakeConstraints { (make) in
            make.edges.equalTo(view)
        }
        
        detailedView?.snp.remakeConstraints { (make) in
            make.edges.equalTo(view)
        }
    }
    
    private func setupMakeLayoutBrief() {
        
        videoView.snp.remakeConstraints { (make) in
            make.left.right.top.equalTo(view)
            make.height.equalTo(67)
        }
        
        briefView?.snp.remakeConstraints { (make) in
            make.edges.equalTo(view)
        }
    }
    
    private func setupMakeUI() {
        style = .detailed
        let color = NSColor.random()
        view.tt_backgroundColor = color
        let color2 = NSColor.black.tt_alpha(0.2)
        videoView.tt_backgroundColor = color2
    }
}
